package com.yazi.mxz.admin.controller;


import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.jackson.result.ObjectResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Notice;
import com.yazi.mxz.common.service.NoticeService;
import com.yazi.mxz.admin.jackson.filter.NoticeFilter;

@Controller("noticeController")
@RequestMapping("/notice")
public class NoticeController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(NoticeController.class);
	
	@Resource
	NoticeService noticeService;
	
	@RequestMapping("/list")
	public String list(Notice notice, Searching si, Model model) {
		log.debug("进入通知列表页面");
		Page<Notice> page = noticeService.list(notice, si);
		PageLink pageLink = new MyPageLink(page.getPaging());
		model.addAttribute("noticeList", page.getList());
		model.addAttribute("pageLink", pageLink);
		model.addAttribute("searchWord", si.getSearchWord());
		return "notice_list";
	}
	
	@RequestMapping("/add")
	public String add(){
		log.debug("进入通知添加页面");
		return "notice_add";
	}
	
	@RequestMapping("/addDo/json")
	@ResponseBody
	public ObjectResult<?> addDo(Notice notice) {
		log.debug("保存新增通知：title={}", notice.getTitle());
		ObjectResult<Notice> jsonResult = new ObjectResult<Notice>();
		jsonResult.setFilter(NoticeFilter.get());
		try{
			noticeService.add(notice);
			jsonResult.setResult(notice);
			jsonResult.setOk();
		} catch (MyException e) {
			log.debug("添加通知失败: {}", e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e){
			log.error("添加通知异常：", e);
			jsonResult.setErrorMessage("添加失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/edit")
	public String edit(Long noticeId, Model model) {
		log.debug("进入通知编辑页面:noticeId={}", noticeId);
		Notice notice = noticeService.one(noticeId);
		model.addAttribute("notice", notice);
		return "notice_edit";
	}
	
	@RequestMapping("/editDo")
	public String editDo(Notice notice, Model model) {
		log.debug("修改通知:noticeId={}", notice.getNoticeId());
		try{
			noticeService.edit(notice);
		} catch (MyException e){
			log.error("修改通知异常：", e);
		}
		return "redirect:/notice/list";
	}
	
	@RequestMapping("/delDo")
	public String delDo(Long noticeId, Model model) {
		log.debug("删除通知：noticeId={}", noticeId);
		try{
			noticeService.remove(noticeId);
		} catch (Exception e){
			log.error("删除通知异常：", e);
		}
		return "redirect:/notice/list";
	}
}
